/**
  ETFRoot project, Anyang Normal University && IMP-CAS
  \class ETFDCTa
  \brief for DCs around the target
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-10-28
  \date 2021-12-10 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2021-10-28   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <iostream>
#include <cstring>
#include <TClonesArray.h>
#include <TGraph.h>
#include "FairRootManager.h"
#include "ETFDetectorManager.h"
#include "ETFDCHit.h"
#include "ETFMath.h"
#include "ETFMsg.h"
#include "ETFDCTa.h"

using std::cout;
using std::endl;

ClassImp(ETFDCTa);

static const double DEGREE = ETFMath::DEGREE();
ETFDCTa::ETFDCTa(const char *name) : ETFVDC(name), type{0}{
  const char xy[] = {'X', 'Y'};
  for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++){
    fHits[i][j] = new TClonesArray("ETFDCHit");
    char name_cl[128]; sprintf(name_cl, "%s%c%d", name, xy[i], j);
    fHits[i][j]->SetName(name_cl);
  } // end embedded for
} // end ctor

ETFDCTa::~ETFDCTa(){
  for(auto &h : fHits) for(auto &l : h) if(l) delete l;
} // end the dtor

void ETFDCTa::Register(){
  static FairRootManager *m = FairRootManager::Instance();
  for(auto &p : fHits) for(auto &l : p) if(l) m->Register(l->GetName(), "DetHits", l, true);
} // end member function Register

TClonesArray *ETFDCTa::GetHitsArray(DCType typeDC, int x12){
  if(x12 < 0 || x12 >= 2) ETFMsg::Error("ETFDC", "GetHitsArray: x12 out of range");
  if(DCType::kV == typeDC) ETFMsg::Error("ETFDC", "GetHitsArray: typeDC out of range");
  return fHits[short(typeDC)%2][x12%2];
} // end member function GetHitsArray

// initialize the detector
void ETFDCTa::Initialize(){
  ETFDetector::Initialize();
  for(auto &h : fHits) for(auto &l : h) l->Clear();
} // end member function Initialize

void ETFDCTa::DrawHits(){
  for(auto &h : fHits[0]) if(h) for(const auto &p : *h)
    dynamic_cast<ETFDCHit *>(p)->Draw();
} // end member function DrawHits

// set the wire's projection to its normal plane
void ETFDCTa::SetHitProjection(ETFDCHit *hit, DCType typeDC){

  if(DCType::kX == typeDC) hit->SetProjection(hit->GetPosition()); // use as it is
  if(DCType::kY == typeDC){
    double proj[3]{}; // projection of the wire center position to the wire's normal plane
    proj[0] = hit->GetY(); // the Y value would be used in place of X here
    proj[1] = hit->GetX();
    proj[2] = hit->GetZ();
    hit->SetProjection(proj);
  } // end if
} // end member function SetHitProjection

void ETFDCTa::Print(){
  ETFDetector::Print();
  static const char *dcType[] = {"DCType::kX", "DCType::kY"};
  static const char *x12[] = {"X1", "X2"};
  int i = 0, j = 0;
  cout << "Number of sense wires per layer: " << NL << endl;
  for(auto &h : fHits){
    cout << "\033[33;1m___________DCType: " << dcType[i++] << "___________\033[0m" << endl;
    j = 0;
    for(auto &l : h){
      cout << "\033[31;1m-------------- x12: " << x12[j++] << " --------------\033[0m" << endl;
      for(auto p : *l) dynamic_cast<ETFDCHit *>(p)->Print();
    } // end for over x1-x2
  } // end for over xy
} // end member function Print
